package com.sise.thread.lab01;

import java.util.Queue;

public class Consumer extends Thread{

    private final Queue sharedQ; // 用于消费的队列
    public int result;
    public Consumer(Queue sharedQ) {
        super("consumer");
        this.sharedQ = sharedQ;
    }

    @Override
    public void run(){
        System.out.println("this is consumer");
        while(true){
            synchronized (sharedQ){
                while(sharedQ.size()==0){
                    try {
                        System.out.println("---------------消费者进入等待中");
                        sharedQ.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                result = (int) sharedQ.poll();
                System.out.println("----------消费掉相应的产品"+result);
                sharedQ.notify();
            }
            if(result==3){
                break;
            }
        }
    }
}
